package com.yanjiali.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yanjiali.entity.SettingContentUser;
import com.yanjiali.enumerate.SettingContentTypeEnum;
import com.yanjiali.model.R;
import com.yanjiali.model.req.UpdateSettingContentReq;
import com.yanjiali.model.resp.UpdateSettingContentResp;
import com.yanjiali.service.SettingContentUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yanjiali.entity.SettingContent;
import com.yanjiali.mapper.SettingContentMapper;
import com.yanjiali.service.SettingContentService;
/**
* @Package: com.yanjiali.service.impl
* @Author: yanjiali
* @Created: 2025/4/30 15:01
*/
@Service
public class SettingContentServiceImpl extends ServiceImpl<SettingContentMapper, SettingContent> implements SettingContentService{

    @Autowired
    private SettingContentUserService settingContentUserService;

    @Override
    public R<UpdateSettingContentResp> updateSettingContent(UpdateSettingContentReq updateSettingContentReq) {
        String userId = "";
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if(authentication != null) {
            userId = authentication.getPrincipal().toString();
        }else {
            throw new RuntimeException("用户未登录，无法修改设置");
        }

        final int status = updateSettingContentReq.getStatus() ? 1 : 0; //用户传递的状态

        LambdaQueryWrapper<SettingContentUser> lqw = new LambdaQueryWrapper<>();
        lqw
                .eq(SettingContentUser::getSetContentId, updateSettingContentReq.getId())
                .eq(SettingContentUser::getUid, userId);
        SettingContentUser settingContentUser = settingContentUserService.getOne(lqw);
        if(settingContentUser == null) {    //没有记录是插入

            settingContentUser = new SettingContentUser();
            settingContentUser.setSetContentId(updateSettingContentReq.getId());
            settingContentUser.setUid(Long.valueOf(userId));
            settingContentUser.setStatus(Byte.valueOf(status + ""));

            boolean save = settingContentUserService.save(settingContentUser);
            if(save) {
                return R.ok(new UpdateSettingContentResp(status == 1));
            }
        }else { //有记录是修改

            LambdaUpdateWrapper<SettingContentUser> luw = new LambdaUpdateWrapper<>();
            luw
                    .eq(SettingContentUser::getSetContentId, updateSettingContentReq.getId())
                    .eq(SettingContentUser::getUid, userId)
                    .set(SettingContentUser::getStatus, status);

            boolean update = settingContentUserService.update(luw);
            if(update) {
                return R.ok(new UpdateSettingContentResp(status == 1));
            }
        }

        //将原本状态返回
        return R.fail(new UpdateSettingContentResp(updateSettingContentReq.getStatus()));
    }

    @Override
    public R getSettingContentIsOpenByName(String name) {
        String userId = "";
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if(authentication != null) {
            userId = authentication.getPrincipal().toString();
        }else {
            throw new RuntimeException("用户未登录，无法修改设置");
        }

        for(SettingContentTypeEnum s : SettingContentTypeEnum.values()) {
            if(s.getName().equals(name)) {
                LambdaQueryWrapper<SettingContent> lqw = new LambdaQueryWrapper<>();
                lqw.eq(SettingContent::getId, s.getSettingContentId());
                SettingContent settingContent = getOne(lqw);
                if(settingContent != null) {
                    LambdaQueryWrapper<SettingContentUser> lambdaQueryWrapper = new LambdaQueryWrapper();
                    lambdaQueryWrapper
                            .eq(SettingContentUser::getSetContentId, settingContent.getId())
                            .eq(SettingContentUser::getUid, userId);
                    SettingContentUser settingContentUser = settingContentUserService.getOne(lambdaQueryWrapper);
                    if(settingContentUser == null) {    //为空就是用户目前没有设置过，默认为false
                        return R.ok(false);
                    }else {
                        return R.ok(settingContentUser.getStatus() == 1);
                    }
                }
            }
        }

        throw new IllegalArgumentException("不合法的设置名");
    }
}
